package net.wanho.controller;

import cn.hutool.crypto.SecureUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.dto.*;
import net.wanho.po.Role;
import net.wanho.po.UserInfo;
import net.wanho.service.AuthInfoService;
import net.wanho.service.RoleService;
import net.wanho.service.UserInfoService;
import net.wanho.service.UserRoleService;
import net.wanho.util.ThreadUtil;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.MenuVo;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserInfoController {
    private final UserInfoService userInfoService;

    private final AuthInfoService  authInfoService;

    private final UserRoleService userRoleService;

    private final RoleService roleService;
    @GetMapping("/auth-list")
    public AjaxResult authList() {
        List<MenuVo> menuVo = authInfoService.findMenus();
        return AjaxResult.success(menuVo);
    }

    @GetMapping("/user-list")
    public AjaxResult userList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               UserInfoDTO userInfoDTO) {
        PageInfo<UserInfo> pageInfo = userInfoService.listPage(pageNum,pageSize,userInfoDTO);
        return AjaxResult.success(pageInfo);
    }

    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody AddDTO addDTO) {
        userInfoService.addUser(addDTO);
        return AjaxResult.success();
    }

    @PutMapping("/updateState")
    public AjaxResult updateState(@RequestBody UserInfo userInfo) {
        userInfoService.updateState(userInfo);
        return AjaxResult.success();
    }

    @GetMapping("/{id}")
    public AjaxResult getUser(@PathVariable Integer id) {
        UserInfo userInfo = userInfoService.getById(id);
        return AjaxResult.success("操作成功",userInfo);
    }

    @PutMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody UserDTO userDTO) {
        userInfoService.updateName(userDTO);
        return AjaxResult.success("用户修改成功！");
    }

    @DeleteMapping("/deleteUser/{id}")
    public AjaxResult deleteUser(@PathVariable Integer id) {
        userInfoService.removeById(id);
        userRoleService.removeByUserId(id);
        return AjaxResult.success("用户删除成功！");
    }

    @DeleteMapping("/deleteUserList")
    public AjaxResult deleteUserList(@RequestBody List<Integer> ids) {
        userInfoService.removeByIds(ids);
        return AjaxResult.success("用户批量删除成功！");
    }

    @PutMapping("/updatePwd/{id}")
    public AjaxResult updatePwd(@PathVariable Integer id) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(id);
        userInfo.setUserPwd(SecureUtil.md5("123456"));
        userInfoService.updateById(userInfo);
        return AjaxResult.success("密码重置成功！");
    }

    @GetMapping("/user-role-list/{id}")
    public AjaxResult userRoleList(@PathVariable Integer id) {
        List<Role> roleList = roleService.selectByUserId(id);
        return AjaxResult.success("success",roleList);
    }

    @PutMapping("/assignRole")
    public AjaxResult assignRole(@RequestBody AssignRoleDTO assignRoleDTO) {
        userRoleService.assignRole(assignRoleDTO);
        return AjaxResult.success("分配角色成功！");
    }

    @GetMapping("/user-auth")
    public AjaxResult userAuth() {
        Integer userId = ThreadUtil.getId();
        List<Integer> authIds = authInfoService.selectByUserId(userId);
        return AjaxResult.success(authIds);
    }

    @SneakyThrows
    @GetMapping("/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  UserInfoDTO userInfoDTO) {
        PageInfo<UserInfo> pageInfo = userInfoService.listPage(pageNum,pageSize,userInfoDTO);

        return AjaxResult.success("success",pageInfo.getResultList());

    }

    @PutMapping("/modifyPwd")
    public AjaxResult modifyPwd(@RequestBody PwdDTO pwdDTO) {
        userInfoService.updatePwd(pwdDTO);
        return AjaxResult.success("密码修改成功！");
    }

    @PutMapping("/modifySelf")
    public AjaxResult modifySelf(@RequestBody MsgDTO msgDTO) {
        userInfoService.updateMsg(msgDTO);
        return AjaxResult.success("修改个人信息成功！");
    }

    @PostMapping("/modifyAvatar/{id}")
    public AjaxResult modifyAvatar(@PathVariable Integer id, MultipartFile file) {
        String url = userInfoService.ModifyAvatar(id,file);
        return AjaxResult.success("上传成功",url);
    }
}
